package interfaces.impl;

import interfaces.ActionInterface;
import interfaces.AgentInterface;

import java.util.ArrayList;

import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;
import constants.Constants;

public abstract class Agent implements AgentInterface {
	
	protected int id;
	protected String name;
	
	protected ArrayList<ActionInterface> listActions;
	protected ArrayList<SpetialFeature> listFeatures;
	
	protected int priority;
	protected boolean isActive=true;	
	
	public  Agent(){
		
		listActions= new ArrayList<ActionInterface>();
		listFeatures = new ArrayList<SpetialFeature>();
		priority= Constants.NO_PRIORITY;
		name="AGENT";
		id = Constants.NO_OBJECT_REFERENCE;
		isActive=true;
		initializeActionList();
		initializeSpecialFeatures();
		initializeName();
		initializeId();

	}
	
	public void addAction(Action action){
		listActions.add(action);
	}
	
	public void addSpetialFeature(SpetialFeature feature){
		listFeatures.add(feature);
	}
	
	public boolean isActive() {
		return isActive;
	}

	public boolean canUpdateSpecialFeature(WorldObject object){
		boolean found;
		ArrayList<SpetialFeature> list=new ArrayList<SpetialFeature>();
		for(SpetialFeature i : listFeatures){
			found=false;
			if(object.getListFeatures().isEmpty()){
				return false;
			}
			i.getId();
	
			for(SpetialFeature j : object.getListFeatures()){
				j.getId();
				if(i.getId()==j.getId()){
					i.setValue(j.getValue());
					
					list.add(j);
					found=true;
					break;
				}
			}
			if(!found){
				return false;
			}
		}
		//listFeatures=list;
		return true;
	}

	public ArrayList<ActionInterface> getListActions() {
		return listActions;
	}

	public void setListActions(ArrayList<ActionInterface> listActions) {
		this.listActions = listActions;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	public ArrayList<ActionInterface> getActionsList(){
		return listActions;
	}
	
	public void getActionsList(ArrayList<ActionInterface> actionList){
		this.listActions=actionList;
	}

	public void update(WorldObject object, Universe universe, double time){
		if(!canUpdate(object)){
			System.out.println("Me falta alguna accion para poder actuar, soy el agent: "+name);
			return;
		}
		if(!canUpdateSpecialFeature(object)){
			System.out.println("Me falta algun special feature, soy el agent: "+name);
			return;
		}
		if(!isActive){
			return;
		}
		updateChild( object,  universe, time);
		
	}
	
	public int getID() {
		return id;
	}

	public void setID(int id) {
		this.id = id;
	}
	
	public String getName(){
		return name;
	}
	
	public void setName(String name){
		this.name=name;
	}
	
	public boolean canUpdate(WorldObject object){
		boolean found;
		ArrayList<ActionInterface> list=new ArrayList<ActionInterface>();
		for(ActionInterface i : listActions){
			found=false;
			for(ActionInterface j : object.getListActions()){
				
				if(i.getId()==j.getId()){
					list.add(j);
					found=true;
					break;
				}
			}
			if(!found){
				return false;
			}
		}
		listActions=list;
		return true;
	}
	
	public int getPriority() {
		return priority;
	}

	public void setPriority(int priority) {
		this.priority = priority;
	}

	public abstract void updateChild(WorldObject object, Universe universe, double time);

	public abstract void initializeActionList();

	public abstract void initializeSpecialFeatures();

	public abstract void initializeName();

	public abstract void initializeId();
	
	
	
}
